﻿#ifndef DBHELPER_H
#define DBHELPER_H

#include <QObject>
#include <QSqlDatabase>
#include "product.h"
#include "userrelation.h"
#include "formulabase.h"

class DbHelper : public QObject
{
    Q_OBJECT
public:
    explicit DbHelper(QObject *parent = nullptr);
    ~DbHelper();

    bool createConnection(const QString database);

    bool isTableExist(QString tableName);

    void createProductTable();

    void insertProduct(Product &product);

    void updateProduct(Product *product);

    void clearProduct();

    Product* getProductById(QString productId);

    QList<Product*> getAllProducts();

    QList<Product*> queryProducts(
            QString overallQuery,
            QString typeQuery,
            QString nameQuery,
            QString primaryQuery,
            QString secondaryQuery);

    QList<Product*> queryPlanProducts();

    QList<Product*> getSanbaoProducts();

    void createUserRelationTable();

    void insertUserRelation(UserRelation &relation);

    void clearUserRelation();

    UserRelation* getUserRelation(QString sourceUsername, QString targetUsername, UserRelation::RelationType type);

    void clearUserRelationByType(UserRelation::RelationType type);

    QList<UserRelation*> getAllUserRelationsByType(UserRelation::RelationType type);

    QList<UserRelation*> queryUserRelation(
            UserRelation::RelationType type,
            QString overallQuery,
            QString nickNameQuery,
            QString contactQuery,
            QString telQuery,
            QString primaryQuery,
            QString secondaryQuery);

    void createFormulaTable();

    void insertFormula(FormulaBase &formulaBase);

    void clearFormula();

    QList<FormulaBase*> getAllFormulas();
signals:

public slots:

private:
    QSqlDatabase db;
    QString database = "data.db";
};

#endif // DBHELPER_H
